﻿// -----------------------------------------------------------------------
// <copyright file="NPContainer.cs" company="Accenture">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NPSoft.Ferrero.Framework.Infrastructure.AOP
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using NPSoft.Ferrero.Framework.Infrastructure.Crosscutting;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class NPContainer : INPContainer
    {
        private static IUnityContainer _container;

        /// <summary>
        /// 构造函数。
        /// </summary>
        public NPContainer()
        {
            _container = new UnityContainer()
            .AddNewExtension<Interception>();
        }

        /// <summary>
        /// 解析类型。
        /// </summary>
        /// <typeparam name="T">源类型</typeparam>
        /// <returns>目标类型</returns>
        public T Resolve<T>()
        {
            return _container.Resolve<T>();
        }

        /// <summary>
        /// 注册类型。
        /// </summary>
        /// <typeparam name="TFrom">源类型</typeparam>
        /// <typeparam name="TTo">目标类型</typeparam>
        public void RegisterType<TFrom, TTo>() where TTo : TFrom
        {
            _container.RegisterType<TFrom, TTo>();
        }

        /// <summary>
        /// 拦截接口。
        /// </summary>
        /// <typeparam name="TFrom">接口类型</typeparam>
        /// <typeparam name="TTo">处理类型</typeparam>
        /// <typeparam name="TBehavior">拦截类型</param>
        public void SetDefaultInterceptorFor<TFrom, TTo, TBehavior>()
            where TTo : TFrom
            where TBehavior : IInterceptionBehavior
        {
            _container.RegisterType<TFrom, TTo>(
                new Interceptor<InterfaceInterceptor>(),
               new InterceptionBehavior<TBehavior>());
        }
    }
}
